# set up environment ----
library(data.table)
library(ggplot2)
library(Hmisc)
library(gridExtra)
source("R/00-functions.R")
load("data/analysis_data.RData")

# type2: Cox PH ranking
respondent_candidate_data[, status := 1]
model1 <- coxph(
  Surv(ranking, status) ~ preferences + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model2 <- coxph(
  Surv(ranking, status) ~ electability + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model3 <- coxph(
  Surv(ranking, status) ~ expected_utility + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)

# type3 = Cox PH reverse ranking
model1a <- coxph(
  Surv(8-ranking, status) ~ preferences + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model2a <- coxph(
  Surv(8-ranking, status) ~ electability + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model3a <- coxph(
  Surv(8-ranking, status) ~ expected_utility + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)

# which direction should we expect to be better? type 3 has lowest AICs
mapply(`-`,
  lapply(list(model1, model2, model3), AIC),
  lapply(list(model1a, model2a, model3a), AIC))

residuals <- cbind(model1a$residuals, model2a$residuals, model3a$residuals)
respondent_types_a <- do.call(rbind, lapply(1:nrow(respondent_data), function(x) {
  y <- residuals[((x - 1) * 7 + 1):(x * 7), ]
  SSR <- colSums(y ^ 2)
  exp(-SSR) / sum(exp(-SSR))
  #which.min(SSR)
}))


# type2: Cox PH ranking
respondent_candidate_data <- respondent_candidate_data[n_attention_fails >= 1 &
    trump_least_preferred == 1 &
    support_2016 != "Trump"]
model1 <- coxph(
  Surv(ranking, status) ~ preferences + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model2 <- coxph(
  Surv(ranking, status) ~ electability + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model3 <- coxph(
  Surv(ranking, status) ~ expected_utility + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)

# type3 = Cox PH reverse ranking
model1a <- coxph(
  Surv(8-ranking, status) ~ preferences + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model2a <- coxph(
  Surv(8-ranking, status) ~ electability + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)
model3a <- coxph(
  Surv(8-ranking, status) ~ expected_utility + strata(subject_id),
  data = respondent_candidate_data,
  id = subject_id)

# which direction should we expect to be better? type 3 has lowest AICs
mapply(`-`,
  lapply(list(model1, model2, model3), AIC),
  lapply(list(model1a, model2a, model3a), AIC))

respondent_data[, chisq.test(table(type2, type3))]


respondent_data[, main := as.numeric(
  n_attention_fails >= 1 &
    trump_least_preferred == 1 &
    support_2016 != "Trump"
)]
respondent_data[main == 0 & order %like% "^E", .(
  mean(type3 == "preferences"),
  mean(type3 == "electability"),
  mean(type3 == "expected_utility")
)]
respondent_data[main == 0 & order %like% "^P", .(
  mean(type3 == "preferences"),
  mean(type3 == "electability"),
  mean(type3 == "expected_utility")
)]






exp(-colSums(head(residuals, 7) ^ 2)) / sum(exp(-colSums(head(residuals, 7) ^ 2)))
